﻿#region

using System;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using BusinessObjects;

#endregion

namespace FastTrackCabService.CustomerPanel
{
    public partial class Profile : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!IsPostBack)
                {
                    var subsidiaries = SubsidiaryBLL.GetAllSubsidiariesWithName();
                    ddlBranch.DataTextField = "BranchName";
                    ddlBranch.DataValueField = "SubsidiaryId";
                    ddlBranch.DataSource = subsidiaries;
                    ddlBranch.DataBind();
                }
                LoadCustomerDetails();
            }
        }

        protected void CustomValidatorEmail_OnServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = HiddenFieldEmail.Value.Equals(txtEmail.Text) || !CustomerBLL.IsEmailTaken(txtEmail.Text);
        }

        protected void btnUpdate_OnClick(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Customer customer = new Customer
                    {
                        CustomerId = Convert.ToInt32(User.Identity.Name)
                    };
                    CustomerBLL.GetCustomerType(customer);
                    if (customer.Type.Equals("Personal"))
                    {
                        var personalCustomer = new PersonalCustomer
                        {
                            CustomerId = customer.CustomerId,
                            FirstName = txtFName.Text,
                            LastName = txtLName.Text,
                            EMail = txtEmail.Text,
                            TelephoneNo = txtTel.Text,
                            Username = txtUname.Text,
                            SubsidiaryId = Convert.ToByte(ddlBranch.SelectedValue)
                        };
                        PersonalCustomerBLL.UpdatePersonalCustomer(personalCustomer);
                    }
                    else
                    {
                        var businessCustomer = new BusinessCustomer
                        {
                            CustomerId = customer.CustomerId,
                            CompanyName = txtCompanyName.Text,
                            EMail = txtEmail.Text,
                            TelephoneNo = txtTel.Text,
                            Username = txtUname.Text,
                            SubsidiaryId = Convert.ToByte(ddlBranch.SelectedValue)
                        };
                        BusinessCustomerBLL.UpdateBusinessCustomer(businessCustomer);
                    }
                }
                catch (Exception)
                {
                    lblStatus.Text = "Something went wrong";
                    lblStatus.ForeColor = Color.Red;
                }
                LoadCustomerDetails();
                lblStatus.Text = "Your profile successfully updated";
                lblStatus.ForeColor = Color.Green;
            }
        }

        private void LoadCustomerDetails()
        {
            var customer = new Customer
            {
                CustomerId = Convert.ToInt32(User.Identity.Name)
            };
            CustomerBLL.GetCustomerType(customer);
            if (customer.Type.Equals("Personal"))
            {
                panelBusinessCustomer.Visible = false;
                panelPersonalCustomer.Visible = true;
                PersonalCustomer personalCustomer = new PersonalCustomer
                {
                    CustomerId = customer.CustomerId
                };
                PersonalCustomerBLL.GetPersonalCustomerDetails(personalCustomer);
                txtFName.Text = personalCustomer.FirstName;
                txtLName.Text = personalCustomer.LastName;
                txtUname.Text = personalCustomer.Username;
                txtEmail.Text = personalCustomer.EMail;
                HiddenFieldEmail.Value = personalCustomer.EMail;
                txtTel.Text = personalCustomer.TelephoneNo;
                ddlBranch.SelectedValue = personalCustomer.SubsidiaryId.ToString();
            }
            else
            {
                panelBusinessCustomer.Visible = true;
                panelPersonalCustomer.Visible = false;
                BusinessCustomer businessCustomer = new BusinessCustomer
                {
                    CustomerId = customer.CustomerId
                };
                BusinessCustomerBLL.GetBusinessCustomerDetails(businessCustomer);
                txtCompanyName.Text = businessCustomer.CompanyName;
                txtUname.Text = businessCustomer.Username;
                txtEmail.Text = businessCustomer.EMail;
                HiddenFieldEmail.Value = businessCustomer.EMail;
                txtTel.Text = businessCustomer.TelephoneNo;
                ddlBranch.SelectedValue = businessCustomer.SubsidiaryId.ToString();
            }
        }

        protected void btnReset_OnClick(object sender, EventArgs e)
        {
            LoadCustomerDetails();
        }
    }
}